
#include "CFont.h"


CFont::CFont()
{
	m_nImageid = -1;
	m_nCharW = 32;
	m_nCharH = 32;
	m_nNumcols = 10;

	m_cStartchar = 32;
}

CFont::~CFont()
{
}

void CFont::load()
{
	m_nImageid = CSGD_TextureManager::GetInstance()->LoadTexture("resource/graphics/SGD_font.png", D3DCOLOR_XRGB(0, 0, 0));
}

RECT CFont::CellAlgorithm(int nID)
{
	RECT rCell;

	rCell.left = (nID % m_nNumcols) * m_nCharW;
	rCell.top = (nID / m_nNumcols) * m_nCharH;
	rCell.right = rCell.left + m_nCharW;
	rCell.bottom = rCell.top + m_nCharH;

	return rCell;
}

void CFont::DrawString(char* szText, int nposx, int nposy, float fscaleX, float fscaleY,
		float fRotation,  DWORD dwcolor)
{
	// Get the str length of string
	int nlen = strlen(szText);
	int printx = nposx;
	int printy = nposy;
	// loop through string 1 char at a time
	for(int i =0; i < nlen; i++)
	{

		// extract ascii value of char we are on
		char ch = szText[i];
		//support space
		if(ch == ' ')
		{
			printx += m_nCharW * fscaleX;
			continue;
		}
		else if(ch == '\n')
		{
			printx = nposx;
			printy += m_nCharH * fscaleY;
			continue;
		}
		// subtract ascii value from start char to make it an id
		int nID = (int)(ch - m_cStartchar);
		//get rect of char to print out
		RECT cLetter = CellAlgorithm(nID);
		CSGD_TextureManager::GetInstance()->Draw(m_nImageid, printx, printy
			, fscaleX, fscaleY, &cLetter, (cLetter.left + (m_nCharW/2))
			, (cLetter.top + (m_nCharH/2))	, fRotation	,dwcolor);
		printx += m_nCharW * fscaleX;
	}

}